package com.sywb.chuangyebao.contract;

import android.content.Context;
import android.media.MediaPlayer;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.czt.mp3recorder.MP3Player;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.Answer;
import com.sywb.chuangyebao.bean.Page;
import com.sywb.chuangyebao.bean.Question;
import com.sywb.chuangyebao.bean.User;
import com.sywb.chuangyebao.utils.DateTimeUtils;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.AnswerDetailActivity;
import com.sywb.chuangyebao.view.AskActivity;
import com.sywb.chuangyebao.view.TopicActivity;
import com.sywb.chuangyebao.view.dialog.ShareDialog;
import com.sywb.chuangyebao.widget.MenuPopup;
import com.sywb.chuangyebao.widget.RichTextView;

import org.bining.footstone.adapter.BaseRecyclerMultiItemAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemChildClickListener;
import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.view.BaseActivity;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public interface QAndADetailContract {

    interface QADetailView extends BaseRefreshContract.View {

        int getQuestionId();

        void setCollectionChanged(Boolean isCollect);

        void showAnimator();

        void dismissAnimator();
    }

    class Presenter extends BaseRefreshContract.Presenter<QADetailView> implements View.OnClickListener {

        private View headView;
        private ImageView ivCollectionUser;
        private ImageView ivUserType;
        private RelativeLayout rlUser;
        private TextView tvCollectionName;
        private TextView tvCollectionTime;
        private TextView tvAddFocus;
        private RichTextView richTextView;
        private ImageView ivImg0;
        private ImageView ivImg1;
        private ImageView ivImg2;
        private LinearLayout recyclerPic;

        private ImageView ivMedia;
        private ImageView ivMediaType;
        private SeekBar seekBar;
        private TextView tvAudioTime;
        private TextView tvCurrentTime;
        private LinearLayout llMedia;

//        private AudioSeekBar audioSeekBar;


        private TextView tvAnswerNum;
        private TextView tvAnswerSort;
        private LinearLayout llImg;

        private MenuPopup menuPopup;
        protected AnswerAdapter recyclerAdapter;
        private Question question;
        public int pageNo = 1;
        private long askId;
        private String sort;
        private String playingAudioUrl;

        @Override
        public void onStart() {
            super.onStart();

            askId = mView.getQuestionId();

            recyclerAdapter = new AnswerAdapter(mActivity);
            setRecyclerAdapter(recyclerAdapter);

            // 防止与ScrollView的滑动冲突
            // commonRecycler.setNestedScrollingEnabled(false);

            headView = mActivity.getLayoutInflater().inflate(R.layout.layout_qanda_detail_head, null);
            // headView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            ivCollectionUser = headView.findViewById(R.id.iv_collection_user);
            ivUserType = headView.findViewById(R.id.iv_user_type);
            tvCollectionName = headView.findViewById(R.id.tv_collection_name);
            tvCollectionTime = headView.findViewById(R.id.tv_collection_time);
            tvAddFocus = headView.findViewById(R.id.tv_add_focus);
            richTextView = headView.findViewById(R.id.richTextView);
            ivImg0 = headView.findViewById(R.id.iv_img0);
            ivImg1 = headView.findViewById(R.id.iv_img1);
            ivImg2 = headView.findViewById(R.id.iv_img2);

            tvAnswerNum = headView.findViewById(R.id.tv_answer_num);

            tvAnswerSort = headView.findViewById(R.id.tv_answer_sort);
            llImg = headView.findViewById(R.id.ll_img);

            llMedia = headView.findViewById(R.id.ll_media);
            ivMedia = headView.findViewById(R.id.iv_media);
            tvAudioTime = headView.findViewById(R.id.tv_audio_time);
            ivMediaType = headView.findViewById(R.id.iv_media_type);
            ivMediaType.setOnClickListener(this);
            seekBar = headView.findViewById(R.id.seekbar);
            tvCurrentTime = headView.findViewById(R.id.tv_current_time);
            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                    question.progress = seekBar.getProgress();
                    tvCurrentTime.setText(DateTimeUtils.getMediaTimeNumber(question.progress / 1000));
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });

            tvAnswerSort.setOnClickListener(this);
            tvAddFocus.setOnClickListener(this);

            ivImg0.setOnClickListener(this);
            ivImg1.setOnClickListener(this);
            ivImg2.setOnClickListener(this);
            headView.findViewById(R.id.rl_user).setOnClickListener(this);
            headView.findViewById(R.id.ll_user_head).setOnClickListener(this);

//            audioSeekBar = headView.findViewById(R.id.audio_seekBar);


            headView.setVisibility(View.GONE);
            headView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
            recyclerAdapter.setHeaderView(headView);

            initPopWindow();
            initData();
        }

        @Override
        public void initData() {
            super.initData();
            getQuestion();
        }

        @Override
        public void onStartRefresh() {
            pageNo = 1;
            listAnswers();
        }

        @Override
        public void onStartLoadMore() {
            pageNo++;
            listAnswers();
        }

        public void getQuestion() {
            HttpUtils.getQuestion(askId, new HttpCallback<Question>() {

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                }

                @Override
                public void onSuccess(Question model) {
                    if (model == null) {
                        return;
                    }

                    question = model;
                    headView.setVisibility(View.VISIBLE);

                    onStartRefresh();

                    //设置提问人头像
                    GlideUtils.getCircleImageToUrl(mActivity, model.getShowAvatar(), ivCollectionUser, R.drawable.head_image);
                    //设置提问人角色类型
                    Utils.setUserRoleIcon(ivUserType, model.getUserRole());
                    //设置提问人名称
                    tvCollectionName.setText(model.getShowName());

                    //设置提问时间
                    tvCollectionTime.setText(DateTimeUtils.getFormatForMinusMonthDayMinutesSecond(model.create_time));

                    //设置是否关注
                    if (question.isMine) {
                        //是自己发布的问题 不显示关注
                        tvAddFocus.setVisibility(View.INVISIBLE);
                    } else {
                        setFollowView();
                    }

                    model.setTopic();
                    //设置富文本
                    richTextView.setExpandView(false);
                    richTextView.setRichText(model.title, model.richInfoList);
                    richTextView.setOnLinkClickListener(new RichTextView.OnLinkClickListener() {
                        @Override
                        public void onAtClick(RichTextView richTextView, Object userId) {
                            Utils.goUserIndex(mView, (Integer) userId);
//                            mView.advance(UserIndexActivity.class, userId);
                        }

                        @Override
                        public void onTopicClick(RichTextView richTextView, Object topicId) {
                            mView.advance(TopicActivity.class, topicId);
                        }

                        @Override
                        public void onEmojiClick(RichTextView richTextView, Object emoji) {

                        }

                        @Override
                        public void onUrlClick(RichTextView richTextView, Object url) {

                        }

                        @Override
                        public void onAllClick(RichTextView richTextView) {

                        }
                    });

                    //设置图片
                    List<String> imageUrls = model.pic;
                    if (isNotNull(imageUrls)) {
                        GlideUtils.getNormalIamgeToUrl(mActivity, model.pic.get(0), ivImg0);
                        if (imageUrls.size() > 1) {
                            GlideUtils.getNormalIamgeToUrl(mActivity, model.pic.get(1), ivImg1);
                            if (imageUrls.size() > 2) {
                                GlideUtils.getNormalIamgeToUrl(mActivity, model.pic.get(2), ivImg2);
                            } else {
                                ivImg2.setVisibility(View.INVISIBLE);
                            }
                        } else {
                            ivImg1.setVisibility(View.INVISIBLE);
                            ivImg2.setVisibility(View.INVISIBLE);
                        }
                    } else {
                        llImg.setVisibility(View.GONE);
                    }

                    //设置语音
                    if (!TextUtils.isEmpty(model.audio)) {
                        String audioLength;
                        if (model.audioLength.contains(":")) {
                            audioLength = model.audioLength;
                        } else {
                            audioLength = "00:" + (Integer.valueOf(model.audioLength) > 9 ? model.audioLength : "0" + model.audioLength);
                        }

                        tvAudioTime.setText(audioLength);
                        GlideUtils.getNormalIamgeToUrl(mActivity, model.avatar, ivMedia, R.drawable.news_pic_default);
                    } else {
                        llMedia.setVisibility(View.GONE);
                        GlideUtils.getNormalIamgeToUrl(mActivity, model.avatar, ivMedia, R.drawable.image_def);
                    }

                    //设置收藏信息
                    setCollectInfo();

                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }
            });
        }

        private void setCollectInfo() {
            //设置回答和收藏数量
            tvAnswerNum.setText(RegexUtils.getFormatNum(question.answer_num) + "回答       " + RegexUtils.getFormatNum(question.collectCount) + "收藏");
            //设置收藏状态
            mView.setCollectionChanged(question.isCollect);
        }

        public void listAnswers() {
            HttpUtils.listAnswers(askId, sort, pageNo, new HttpCallback<Page<Answer>>() {

                @Override
                public void onStart() {
                    super.onStart();

                }

                @Override
                public void onSuccess(Page<Answer> answerPage) {
                    refreshPage(answerPage);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                    refreshPage(null);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }

        private void refreshPage(Page<Answer> pageInfo) {
//            mView.hideProgress();

            if (pageNo == 1) {
                onEndRefresh();
                recyclerAdapter.clearDatas();
            } else {
                onEndLoadMore();
                recyclerAdapter.closeLoadMore();
            }

            if (pageInfo != null && pageInfo.list != null) {

                recyclerAdapter.notifyDataChangedAfterLoadMore(pageInfo.list);
                // recyclerAdapter.setLoadMoreEnable(pageInfo.page < pageInfo.lastPageNo);
                initType();
            }
            isNoData();
            if (pageNo == 1 && pageInfo != null && recyclerAdapter.getDataCount() >= 3) {
                addConsultItem();
            }
        }

        @Override
        public void isNoData() {
            super.isNoData();
            if (mActivity != null) {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recyclerAdapter.setEmptyView(R.layout.layout_no_answer, true);
                    }
                });
            }
        }

        private void addConsultItem() {
            if (question == null || question.consult == null || recyclerAdapter.getDataCount() <= 2) {
                return;
            }

            Answer answer = new Answer();
            answer.setItemType(AnswerAdapter.LAYOUT_TUTOR);
            recyclerAdapter.addData(2, answer);
        }

        private void initType() {
            for (Answer answer : recyclerAdapter.getDatas()) {

                if (answer.getItemType() == AnswerAdapter.LAYOUT_TUTOR) {
                    continue;
                }

                if (!TextUtils.isEmpty(answer.audio)) {
                    answer.setItemType(AnswerAdapter.LAYOUT_MEDIA);
                    continue;
                }

                if (isNotNull(answer.pic)) {
                    answer.setItemType(AnswerAdapter.LAYOUT_PIC_THREE);
                }
            }
        }

        private void initPopWindow() {
            menuPopup = new MenuPopup(mActivity);
            menuPopup.setItemOnClickListener(new MenuPopup.OnItemOnClickListener() {
                @Override
                public void onItemClick(String item, int position) {
                    tvAnswerSort.setText(item);
                    switch (position) {
                        case 0:
                            sort = "default";
                            break;
                        case 1:
                            sort = "create_time";
                            break;
                    }

                    onStartAsync();
                    initData();
                }
            });
            // 给标题栏弹窗添加子类
            menuPopup.addAction(mActivity.getString(R.string.sort_default));
            menuPopup.addAction(mActivity.getString(R.string.sort_time));
            menuPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    mView.dismissAnimator();
                }
            });
        }

        @Override
        public void onDestroy() {
            if (menuPopup != null) {
                menuPopup.dismiss();
                menuPopup = null;
            }
            if (mp3Player != null) {
                mp3Player.stopPlay();
                mp3Player = null;
            }
            super.onDestroy();
        }

        private MP3Player mp3Player;
        private boolean isPlaying;

        private void playAudio(final String currentAudioUrl) {
            //播放语音
            if (mp3Player == null || TextUtils.isEmpty(playingAudioUrl)) {
                mp3Player = MP3Player.getInstance();
                mp3Player.setOnMP3PlayerListener(new MP3Player.OnMP3PlayerListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        Logger.e("MediaPlayer onCompletion:");
                        initAudioFinished();
                        playStop();
                    }

                    @Override
                    public void onSeekComplete(MediaPlayer mediaPlayer) {
                        Logger.e("MediaPlayer onSeekComplete:");
                    }

                    @Override
                    public void onError(MediaPlayer mediaPlayer) {
                        onCompletion(mediaPlayer);
                        showMessage("播放错误,请稍候再试");
                    }
                });

                mp3Player.setOnMP3PlayerStartListener(new MP3Player.onMP3PlayerStartListener() {
                    @Override
                    public void onStart(MediaPlayer mediaPlayer) {
                        Logger.e("duration:" + mp3Player.getMediaPlayer().getDuration());
                        if (isQuestion) {
                            seekBar.setMax(mp3Player.getMediaPlayer().getDuration());
//                            tvAudioTime.setText(DateTimeUtils.getMediaTimeNumber(seekBar.getMax() / 1000));
                        } else {
                            currentAudioAnswer.max = mp3Player.getMediaPlayer().getDuration();
                            recyclerAdapter.notifyDataSetChanged();
                        }
                        startTimer();
                    }
                });

                mp3Player.startPlay(currentAudioUrl, null);
                isPlaying = true;
                playingAudioUrl = currentAudioUrl;
                return;
            }
            if (currentAudioUrl.equals(playingAudioUrl)) {
                if (isPlaying) {
                    mp3Player.pausePlay();
                    playStop();
                } else {
                    mp3Player.restart();
                    isPlaying = true;
                    startTimer();
                }
            } else {
                if (isPlaying) {
                    mp3Player.stopPlay();
                }

                initAudioFinished();
                playStop();
                mp3Player.startPlay(currentAudioUrl, null);
                isPlaying = true;
//                startTimer();
            }
            playingAudioUrl = currentAudioUrl;
        }

        private void initAudioFinished() {
            if (isQuestion) {
                seekBar.setProgress(0);
                tvCurrentTime.setText("00:00");
            } else {
                currentAudioAnswer.progress = 0;
            }
        }

        public void playStop() {
            isPlaying = false;
            if (isQuestion && ivMediaType != null) {
                ivMediaType.setImageResource(R.drawable.icon_media_audio);
            } else if (recyclerAdapter != null) {
                recyclerAdapter.notifyDataSetChanged();
            }
            stopTimer();
        }


        private Timer mTimer;
        private boolean isChanging = false;//互斥变量，防止定时器与SeekBar拖动时进度冲突
        private Answer currentAudioAnswer;
        private boolean isQuestion;

        private void startTimer() {
            Logger.e("question.progress:" + question.progress);
            if (isQuestion && question.progress > 0) {
                //目前为问题播放
                mp3Player.getMediaPlayer().seekTo(question.progress);
            } else if (currentAudioAnswer != null && currentAudioAnswer.progress > 0) {
                Logger.e("currentAudioAnswer.progress:" + currentAudioAnswer.progress);
                //目前为回答列表播放
                mp3Player.getMediaPlayer().seekTo(currentAudioAnswer.progress);
            }

            //----------定时器记录播放进度---------//
            isChanging = false;
            mTimer = new Timer();
            TimerTask mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    if (isChanging) {
                        return;
                    }

                    try {
                        if (mp3Player.getMediaPlayer() == null) {
                            return;
                        }

                        if (isQuestion) {
                            question.progress = mp3Player.getMediaPlayer().getCurrentPosition();
                            mActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    tvCurrentTime.setText(DateTimeUtils.getMediaTimeNumber(question.progress / 1000));
                                    seekBar.setProgress(question.progress);
                                }
                            });
                        } else {
                            currentAudioAnswer.progress = mp3Player.getMediaPlayer().getCurrentPosition();
                            mActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    recyclerAdapter.notifyDataSetChanged();
                                }
                            });
                        }
                        Logger.e("currentPosition:" + mp3Player.getMediaPlayer().getCurrentPosition());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            mTimer.schedule(mTimerTask, 0, 500);
        }


        private void stopTimer() {
            if (mp3Player != null) {
                mp3Player.pause();
            }
            if (mTimer != null) {
                isChanging = true;
                mTimer.cancel();
                Logger.e("stopTimer");
            }
        }


        @Override
        public void onClick(View view) {
            if (!((BaseActivity) mActivity).isCanClick(view)) return;
            switch (view.getId()) {
                case R.id.iv_media_type:
                    isQuestion = true;
                    playAudio(question.audio);
                    ivMediaType.setImageResource(mp3Player != null && isPlaying ? R.drawable.stop_video_l : R.drawable.icon_media_audio);
                    break;
                case R.id.tv_answer_sort:
                    mView.showAnimator();
                    menuPopup.show(view);
                    break;
                case R.id.rl_user:
                case R.id.ll_user_head:
//                    mView.advance(UserIndexActivity.class, question.user_id);
                    Utils.goUserIndex(mView, question.user_id);
                    break;
                case R.id.tv_add_focus:
                    //判断是否登录
                    if (!Utils.checkIsLogin()) {
                        return;
                    }

                    HttpUtils.follow(question.user_id, Constants.TOPIC_TYPE_USER, !question.isConcern, new HttpCallback<Object>() {

                        @Override
                        public void onStart() {
                            super.onStart();
                            onStartAsync();
                        }

                        @Override
                        public void onFinish() {
                            super.onFinish();
                            onFinishAsync();
                        }

                        @Override
                        public void onSuccess(Object s) {
                            ToastUtils.show(mContext, question.isConcern ? "取消关注" : "关注成功");
                            question.isConcern = !question.isConcern;
                            setFollowView();
                        }

                        @Override
                        public void onError(String strMsg) {
                            super.onError(strMsg);
                            showMessage(strMsg);
                        }
                    });
                    break;
                case R.id.iv_img0:
                    Utils.goBigPic(mActivity, 0, question.pic);
                    break;
                case R.id.iv_img1:
                    Utils.goBigPic(mActivity, 1, question.pic);
                    break;
                case R.id.iv_img2:
                    Utils.goBigPic(mActivity, 2, question.pic);
                    break;
            }
        }

        private void setFollowView() {
            Utils.setFolllowView(mActivity, question.isConcern, tvAddFocus);
        }

        public void showDialog() {
            if (question != null) {
                ShareDialog sharedDialog = ShareDialog.newInstance(Constants.TOPIC_TYPE_QUESTION, question.question_id, question.isMine ? ShareDialog.TYPE_DELETE : ShareDialog.TYPE_REPORT, question.title, question.display_name, question.avatar);
                sharedDialog.show(mView.getMyFragmentManager(), "Shared");
            }
        }

        public void goAnswer() {
            if (Utils.checkIsNext(mView.getMyFragmentManager())) {
                mView.advance(AskActivity.class, AskActivity.COMING_TYPE_ANSWER, JSON.toJSONString(question));
            }
        }

        public void collect() {
            //判断是否登录
            if (!Utils.checkIsNext(mView.getMyFragmentManager())) {
                return;
            }

            HttpUtils.favorite(Constants.TOPIC_TYPE_QUESTION, question.question_id, !question.isCollect, new HttpCallback<Object>() {

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }

                @Override
                public void onSuccess(Object integer) {
                    ToastUtils.show(mContext, question.isCollect ? "取消收藏" : "收藏成功");
                    question.isCollect = !question.isCollect;
                    question.collectCount = question.isCollect ? question.collectCount + 1 : question.collectCount - 1;
                    RxBus.get().post(Constants.POST_SAVE_QUESTION, Constants.POST_SAVE_QUESTION);
                    setCollectInfo();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }
            });
        }

        /**
         * 畅谈通
         */
        public void goConsult() {

//            HttpUtils.getCode(question.project_id, 0, new HttpCallback<String>() {
//
//                @Override
//                public void onStart() {
//                    super.onStart();
//                    onStartAsync();
//                }
//
//                @Override
//                public void onFinish() {
//                    super.onFinish();
//                    onFinishAsync();
//                }
//
//                @Override
//                public void onSuccess(String s) {
//                    if (TextUtils.isEmpty(s)) {
//                        return;
//                    }
//
//                    mView.advance(WebActivity.class, "畅谈通", s);
//                }
//
//                @Override
//                public void onError(String strMsg) {
//                    super.onError(strMsg);
//                    showMessage(strMsg);
//                }
//            });

            if (question != null) {
                Utils.goConsult(this, mView, question.project_id, 0, Constants.COOKIE_QA);
            }
        }


        class AnswerAdapter extends BaseRecyclerMultiItemAdapter<Answer> implements OnItemChildClickListener {

            /**
             * 纯文字布局()
             */
            private static final int LAYOUT_TEXT = 0;
            /**
             * 三张图片布局
             */
            private static final int LAYOUT_PIC_THREE = 2;
            /**
             * 媒体布局(语音)
             */
            private static final int LAYOUT_MEDIA = 1;

            /**
             * 回答第三条
             * 导师在线咨询
             * 若为在线项目话题下面的提问，第三条调取该项目好对应的畅谈通客服；
             * 点击“在线咨询”调取该项目的畅谈通聊天对话框
             * 字段说明
             * 在线项目的导师头像
             * 项目导师名
             * 该项目畅谈通
             * 项目导师简介
             * <p>
             * • 若为非在线项目提问，则调取3158官方客服
             * 3158客服头像：3158logo
             * 官方客服；
             * 公司简介
             */
            private static final int LAYOUT_TUTOR = 3;

            public AnswerAdapter(Context context) {
                super(context, null);
                addItemType(LAYOUT_TEXT, R.layout.item_answer_text);
                addItemType(LAYOUT_PIC_THREE, R.layout.item_answer_pic_three);
                addItemType(LAYOUT_MEDIA, R.layout.item_answer_media);
                addItemType(LAYOUT_TUTOR, R.layout.item_tutor_online);
            }

            @Override
            protected void setMultiData(ViewHolderHelper viewHolderHelper, int i, Answer model) {
                viewHolderHelper.setOnItemChildClickListener(this);

                switch (model.getItemType()) {
                    case LAYOUT_TEXT:
                        setHeadContentBottom(viewHolderHelper, i, model);
                        break;
                    case LAYOUT_PIC_THREE:
                        viewHolderHelper.setItemChildClickListener(R.id.iv_img0);
                        viewHolderHelper.setItemChildClickListener(R.id.iv_img1);
                        viewHolderHelper.setItemChildClickListener(R.id.iv_img2);

                        setHeadContentBottom(viewHolderHelper, i, model);

                        LinearLayout llImg = viewHolderHelper.getView(R.id.ll_img);
                        ImageView img0 = viewHolderHelper.getView(R.id.iv_img0);
                        ImageView img1 = viewHolderHelper.getView(R.id.iv_img1);
                        ImageView img2 = viewHolderHelper.getView(R.id.iv_img2);

                        //设置图片
                        List<String> imageUrls = model.pic;
                        if (isNotNull(imageUrls)) {
                            llImg.setVisibility(View.VISIBLE);
                            GlideUtils.getNormalIamgeToUrl(mActivity, model.pic.get(0), img0);
                            if (imageUrls.size() > 1) {
                                img1.setVisibility(View.VISIBLE);
                                GlideUtils.getNormalIamgeToUrl(mActivity, model.pic.get(1), img1);
                                if (imageUrls.size() > 2) {
                                    img2.setVisibility(View.VISIBLE);
                                    GlideUtils.getNormalIamgeToUrl(mActivity, model.pic.get(2), img2);
                                } else {
                                    img2.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                img1.setVisibility(View.INVISIBLE);
                                img2.setVisibility(View.INVISIBLE);
                            }
                        } else {
                            llImg.setVisibility(View.GONE);
                        }
                        break;
                    case LAYOUT_MEDIA:
                        setHeadContentBottom(viewHolderHelper, i, model);
                        Utils.setAnswerAudioItemView(viewHolderHelper, mActivity, model, playingAudioUrl, mp3Player, isPlaying);
                        break;
                    case LAYOUT_TUTOR:
                        viewHolderHelper.setItemChildClickListener(R.id.iv_user_head);
                        viewHolderHelper.setItemChildClickListener(R.id.iv_user_type);
                        viewHolderHelper.setItemChildClickListener(R.id.tv_collection_name);

                        viewHolderHelper.setTextColor(R.id.tv_collection_name, ContextCompat.getColor(mActivity, R.color.messageRed));

                        TextView textView = viewHolderHelper.getView(R.id.tv_recommend);
                        textView.setTextColor(ContextCompat.getColor(mActivity, R.color.messageRed));
                        textView.setBackgroundResource(R.drawable.round_line_red);
                        int dpH = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_8);
                        int dpV = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_4);
                        textView.setPadding(dpH, dpV, dpH, dpV);

                        viewHolderHelper.setText(R.id.tv_recommend, R.string.consult_online);

                        User consult = question.consult;
                        if (consult == null) {
                            return;
                        }

                        ImageView imageFaceTutor = viewHolderHelper.getView(R.id.iv_user_head);
                        GlideUtils.getCircleImageToUrl(mActivity, consult.getShowAvatar(), imageFaceTutor);

                        viewHolderHelper.setText(R.id.tv_collection_name, consult.getShowName());

                        viewHolderHelper.setText(R.id.tv_collection_time, consult.getGoodAtString());

                        viewHolderHelper.setText(R.id.tv_subtitile, consult.signature);
                        break;
                }
            }


            private void setHeadContentBottom(ViewHolderHelper viewHolderHelper, int i, Answer model) {
                viewHolderHelper.setVisibility(R.id.tv_collection_time, View.GONE);
                //设置回答人头像
                ImageView imageFace = viewHolderHelper.getView(R.id.iv_user_head);
                GlideUtils.getCircleImageToUrl(mActivity, model.avatar, imageFace);

                //设置回答人角色类型
                Utils.setUserRoleIcon((ImageView) viewHolderHelper.getView(R.id.iv_user_type), model.user_role);

                //设置回答人名称
                viewHolderHelper.setText(R.id.tv_collection_name, model.display_name);

                viewHolderHelper.setItemChildClickListener(R.id.iv_user_head);
                viewHolderHelper.setItemChildClickListener(R.id.iv_user_type);
                viewHolderHelper.setItemChildClickListener(R.id.tv_collection_name);

                //设置回答时间
                viewHolderHelper.setText(R.id.tv_recommend, DateTimeUtils.getFormatForMinusMonthDayMinutesSecond(model.create_time));

                //设置富文本
                viewHolderHelper.setItemChildClickListener(R.id.richTextView);
                RichTextView richTextView = viewHolderHelper.getView(R.id.richTextView);
                richTextView.setRichText(model.content, model.richInfoList);
                richTextView.setVisibility(TextUtils.isEmpty(model.content) ? View.GONE : View.VISIBLE);
                richTextView.setOnLinkClickListener(new RichTextView.OnLinkClickListener() {
                    @Override
                    public void onAtClick(RichTextView richTextView, Object userId) {
//                        mView.advance(UserIndexActivity.class, userId);
                        Utils.goUserIndex(mView, (Integer) userId);
                    }

                    @Override
                    public void onTopicClick(RichTextView richTextView, Object topicId) {
                        mView.advance(TopicActivity.class, topicId);
                    }

                    @Override
                    public void onEmojiClick(RichTextView richTextView, Object emoji) {

                    }

                    @Override
                    public void onUrlClick(RichTextView richTextView, Object url) {

                    }

                    @Override
                    public void onAllClick(RichTextView richTextView) {

                    }
                });

                //设置赞和评论个数
                viewHolderHelper.setText(R.id.tv_bottom_left, RegexUtils.getFormatNum(model.agree_num) + "赞  ·   " + RegexUtils.getFormatNum(model.answer_num) + "评论");
                viewHolderHelper.setVisibility(R.id.tv_bottom_right, View.GONE);

            }

            @Override
            public void onItemChildClick(View view, int i) {
                if (i == 0) {
                    return;
                }

                Answer answer = mDatas.get(i - 1);
                switch (view.getId()) {
                    case R.id.iv_user_head:
                    case R.id.iv_user_type:
                    case R.id.tv_collection_name:
                        Utils.goUserIndex(mView, i == 3 ? question.consult.user_id : answer.user_id);
                        break;
                    case R.id.richTextView:
                        onRecyclerItemClick(i);
                        break;
                    case R.id.iv_media_type:
                        //播放语音
                        currentAudioAnswer = answer;
                        isQuestion = false;
                        playAudio(answer.audio);
                        notifyDataSetChanged();
                        break;
                    case R.id.iv_img0:
                        Utils.goBigPic(mActivity, 0, answer.pic);
                        break;
                    case R.id.iv_img1:
                        Utils.goBigPic(mActivity, 1, answer.pic);
                        break;
                    case R.id.iv_img2:
                        Utils.goBigPic(mActivity, 2, answer.pic);
                        break;
                }
            }
        }


        public void onRecyclerItemClick(int i) {
            if (recyclerAdapter.getDataCount() == 0 || i == 0) {
                return;
            }
            Answer answer = recyclerAdapter.getItem(i - 1);
            if (answer.getItemType() == AnswerAdapter.LAYOUT_TUTOR) {
//                User tutor = question.consult;
//                if (tutor != null) {
//                    mView.advance(AskActivity.class, AskActivity.COMING_TYPE_ASK, "", tutor.display_name, tutor.user_id);
//                }
                goConsult();
            } else {
                mView.advance(AnswerDetailActivity.class, answer.answer_id);
            }
        }
    }


}

